#include "parampreviewdlg.h"
#include "nodemanager/wnodemanager.h"
#include "gui/guimanager.h"

ParamPreviewDlg::ParamPreviewDlg(wxWindow* parent, wxString title)
    : wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxSize(1024, 700),
               wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    // 创建笔记本控件（选项卡）
    m_notebook = new wxNotebook(this, wxID_ANY);

    // 创建四个页面
    m_NodeManagerPanel = new NodeManagerPanel(m_notebook);
    m_SectManagerPanel = new SectManagerPanel(m_notebook);
    m_hostilerelationspanel = new hostilerelationspanel(m_notebook);
    m_GuiParamManagerPanel = new GuiParamManagerPanel(m_notebook);

    // 创建图像列表
    m_imageList = new wxImageList(16, 16);

    // 添加图标到图像列表（使用系统提供的图标）
    m_imageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_OTHER, wxSize(16, 16)));
    m_imageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16, 16)));
    m_imageList->Add(wxArtProvider::GetBitmap(wxART_TICK_MARK, wxART_OTHER, wxSize(16, 16)));
    m_imageList->Add(wxArtProvider::GetBitmap(wxART_TIP, wxART_OTHER, wxSize(16, 16)));

    // 分配图像列表给笔记本
    m_notebook->AssignImageList(m_imageList);

    // 添加页面到笔记本
    m_notebook->AddPage(m_NodeManagerPanel, wxT("结点"));
    m_notebook->SetPageImage(0, 0);
    m_notebook->AddPage(m_SectManagerPanel, wxT("区段"));
    m_notebook->SetPageImage(1, 1);
    m_notebook->AddPage(m_hostilerelationspanel, wxT("敌对关系"));
    m_notebook->SetPageImage(2, 2);
    m_notebook->AddPage(m_GuiParamManagerPanel, wxT("GUI"));
    m_notebook->SetPageImage(3, 3);

    // 设置主布局
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
    mainSizer->Add(m_notebook, 1, wxEXPAND | wxALL, 5);

    this->Bind(wxEVT_CLOSE_WINDOW, &ParamPreviewDlg::OnClose, this);

    this->SetSizer(mainSizer);
    this->CenterOnScreen();

    // 导入所有数据
    this->loadAllData();
}

ParamPreviewDlg::~ParamPreviewDlg()
{

}

void ParamPreviewDlg::loadAllData(void)
{
    // 导入所有的结点
    std::map<wxString,WBaseNode*> decNodes = WNodeManager::getSingleton().getAllNodes();
    if(decNodes.empty())
        return;

    std::map<wxString,WBaseNode*>::iterator iter = decNodes.begin();
    for(;iter != decNodes.end();++iter)
    {
        m_NodeManagerPanel->addBaseNode((*iter).second);
    }

    // 导入所有的区段
    std::map<wxString,tagSectNode*> decsects = WNodeManager::getSingleton().getAllSects();
    if(decsects.empty())
        return;

    std::map<wxString,tagSectNode*>::iterator itersect = decsects.begin();
    for(;itersect != decsects.end();++itersect)
    {
        m_SectManagerPanel->addSect((*itersect).second);
    }

    // 导入所有的敌对关系
    std::map<wxString,std::map<wxString,tagRouteCondiItem>> allroutecondis = CRouteConditions::getSingleton().getAllRouteConditions();
    if(allroutecondis.empty())
        return;

    std::map<wxString,std::map<wxString,tagRouteCondiItem>>::iterator itercondi = allroutecondis.begin();
    for(;itercondi != allroutecondis.end();++itercondi)
    {
        std::map<wxString,tagRouteCondiItem>::iterator itercondiitem = (*itercondi).second.begin();
        for(;itercondiitem != (*itercondi).second.end();++itercondiitem)
        {
            m_hostilerelationspanel->addRouteCondiItem(&(*itercondiitem).second);
        }
    }

    // 导入所有的GUI
    std::map<int,GuiBaseNode*> allguibasenodes = GuiManager::getSingleton().getAllBaseNodes();
    std::map<int,GuiBaseNode*>::iterator iterguinode = allguibasenodes.begin();
    for(;iterguinode != allguibasenodes.end();++iterguinode)
    {
        m_GuiParamManagerPanel->addGuiBaseNode((*iterguinode).second);
    }
}

void ParamPreviewDlg::OnClose(wxCloseEvent& event)
{
    WNodeManager::getSingleton().saveJsonConfig();
    event.Skip();
}
